﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace NotepadExpress
{
    public enum FindType
    {
        FindOnly = 0,
        FindAndReplace = 1
    }

    public class FindAndReplace : Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
this.findWhatLabel = new System.Windows.Forms.Label();
this.findComboBox = new System.Windows.Forms.ComboBox();
this.findButton = new System.Windows.Forms.Button();
this.replaceButton = new System.Windows.Forms.Button();
this.replaceAllButton = new System.Windows.Forms.Button();
this.closeButton = new System.Windows.Forms.Button();
this.rangeGroupBox = new System.Windows.Forms.GroupBox();
this.rangeAllFiles = new System.Windows.Forms.RadioButton();
this.rangeWholeFile = new System.Windows.Forms.RadioButton();
this.rangeSelection = new System.Windows.Forms.RadioButton();
this.replaceComboBox = new System.Windows.Forms.ComboBox();
this.replaceWithLabel = new System.Windows.Forms.Label();
this.directionGroupBox = new System.Windows.Forms.GroupBox();
this.directionDown = new System.Windows.Forms.RadioButton();
this.directionUp = new System.Windows.Forms.RadioButton();
this.matchPanel = new System.Windows.Forms.Panel();
this.matchCaseCheckBox = new System.Windows.Forms.CheckBox();
this.matchWordCheckBox = new System.Windows.Forms.CheckBox();
this.rangeGroupBox.SuspendLayout();
this.directionGroupBox.SuspendLayout();
this.matchPanel.SuspendLayout();
this.SuspendLayout();
// 
// findWhatLabel
// 
this.findWhatLabel.AutoSize = true;
this.findWhatLabel.Location = new System.Drawing.Point(12, 9);
this.findWhatLabel.Name = "findWhatLabel";
this.findWhatLabel.Size = new System.Drawing.Size(56, 13);
this.findWhatLabel.TabIndex = 0;
this.findWhatLabel.Text = "Find &what:";
// 
// findComboBox
// 
this.findComboBox.FormattingEnabled = true;
this.findComboBox.Location = new System.Drawing.Point(95, 6);
this.findComboBox.Name = "findComboBox";
this.findComboBox.Size = new System.Drawing.Size(211, 21);
this.findComboBox.TabIndex = 1;
this.findComboBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.findComboBox_KeyDown);
this.findComboBox.TextChanged += new System.EventHandler(this.findComboBox_TextChanged);
// 
// findButton
// 
this.findButton.Location = new System.Drawing.Point(338, 6);
this.findButton.Name = "findButton";
this.findButton.Size = new System.Drawing.Size(75, 23);
this.findButton.TabIndex = 13;
this.findButton.Text = "&Find";
this.findButton.UseVisualStyleBackColor = true;
this.findButton.Click += new System.EventHandler(this.findButton_Click);
// 
// replaceButton
// 
this.replaceButton.Location = new System.Drawing.Point(338, 35);
this.replaceButton.Name = "replaceButton";
this.replaceButton.Size = new System.Drawing.Size(75, 23);
this.replaceButton.TabIndex = 14;
this.replaceButton.Text = "&Replace";
this.replaceButton.UseVisualStyleBackColor = true;
this.replaceButton.Click += new System.EventHandler(this.replaceButton_Click);
// 
// replaceAllButton
// 
this.replaceAllButton.Location = new System.Drawing.Point(338, 64);
this.replaceAllButton.Name = "replaceAllButton";
this.replaceAllButton.Size = new System.Drawing.Size(75, 23);
this.replaceAllButton.TabIndex = 15;
this.replaceAllButton.Text = "Replace &All";
this.replaceAllButton.UseVisualStyleBackColor = true;
this.replaceAllButton.Click += new System.EventHandler(this.replaceAllButton_Click);
// 
// closeButton
// 
this.closeButton.Location = new System.Drawing.Point(338, 92);
this.closeButton.Name = "closeButton";
this.closeButton.Size = new System.Drawing.Size(75, 23);
this.closeButton.TabIndex = 16;
this.closeButton.Text = "&Close";
this.closeButton.UseVisualStyleBackColor = true;
this.closeButton.Click += new System.EventHandler(this.closeButton_Click);
// 
// rangeGroupBox
// 
this.rangeGroupBox.Controls.Add(this.rangeAllFiles);
this.rangeGroupBox.Controls.Add(this.rangeWholeFile);
this.rangeGroupBox.Controls.Add(this.rangeSelection);
this.rangeGroupBox.Location = new System.Drawing.Point(174, 59);
this.rangeGroupBox.Name = "rangeGroupBox";
this.rangeGroupBox.Size = new System.Drawing.Size(153, 86);
this.rangeGroupBox.TabIndex = 9;
this.rangeGroupBox.TabStop = false;
this.rangeGroupBox.Text = "Re&place range";
// 
// rangeAllFiles
// 
this.rangeAllFiles.AutoSize = true;
this.rangeAllFiles.Location = new System.Drawing.Point(23, 55);
this.rangeAllFiles.Name = "rangeAllFiles";
this.rangeAllFiles.Size = new System.Drawing.Size(57, 17);
this.rangeAllFiles.TabIndex = 12;
this.rangeAllFiles.Text = "All f&iles";
this.rangeAllFiles.UseVisualStyleBackColor = true;
// 
// rangeWholeFile
// 
this.rangeWholeFile.AutoSize = true;
this.rangeWholeFile.Location = new System.Drawing.Point(23, 37);
this.rangeWholeFile.Name = "rangeWholeFile";
this.rangeWholeFile.Size = new System.Drawing.Size(72, 17);
this.rangeWholeFile.TabIndex = 11;
this.rangeWholeFile.Text = "Wh&ole file";
this.rangeWholeFile.UseVisualStyleBackColor = true;
// 
// rangeSelection
// 
this.rangeSelection.AutoSize = true;
this.rangeSelection.Checked = true;
this.rangeSelection.Location = new System.Drawing.Point(23, 19);
this.rangeSelection.Name = "rangeSelection";
this.rangeSelection.Size = new System.Drawing.Size(69, 17);
this.rangeSelection.TabIndex = 10;
this.rangeSelection.TabStop = true;
this.rangeSelection.Text = "&Selection";
this.rangeSelection.UseVisualStyleBackColor = true;
// 
// replaceComboBox
// 
this.replaceComboBox.FormattingEnabled = true;
this.replaceComboBox.Location = new System.Drawing.Point(95, 32);
this.replaceComboBox.Name = "replaceComboBox";
this.replaceComboBox.Size = new System.Drawing.Size(211, 21);
this.replaceComboBox.TabIndex = 3;
// 
// replaceWithLabel
// 
this.replaceWithLabel.AutoSize = true;
this.replaceWithLabel.Location = new System.Drawing.Point(12, 35);
this.replaceWithLabel.Name = "replaceWithLabel";
this.replaceWithLabel.Size = new System.Drawing.Size(72, 13);
this.replaceWithLabel.TabIndex = 2;
this.replaceWithLabel.Text = "R&eplace with:";
// 
// directionGroupBox
// 
this.directionGroupBox.Controls.Add(this.directionDown);
this.directionGroupBox.Controls.Add(this.directionUp);
this.directionGroupBox.Location = new System.Drawing.Point(15, 96);
this.directionGroupBox.Name = "directionGroupBox";
this.directionGroupBox.Size = new System.Drawing.Size(153, 49);
this.directionGroupBox.TabIndex = 6;
this.directionGroupBox.TabStop = false;
this.directionGroupBox.Text = "&Direction";
// 
// directionDown
// 
this.directionDown.AutoSize = true;
this.directionDown.Checked = true;
this.directionDown.Location = new System.Drawing.Point(79, 19);
this.directionDown.Name = "directionDown";
this.directionDown.Size = new System.Drawing.Size(53, 17);
this.directionDown.TabIndex = 8;
this.directionDown.TabStop = true;
this.directionDown.Text = "&Down";
this.directionDown.UseVisualStyleBackColor = true;
// 
// directionUp
// 
this.directionUp.AutoSize = true;
this.directionUp.Location = new System.Drawing.Point(23, 19);
this.directionUp.Name = "directionUp";
this.directionUp.Size = new System.Drawing.Size(39, 17);
this.directionUp.TabIndex = 7;
this.directionUp.Text = "&Up";
this.directionUp.UseVisualStyleBackColor = true;
// 
// matchPanel
// 
this.matchPanel.Controls.Add(this.matchCaseCheckBox);
this.matchPanel.Controls.Add(this.matchWordCheckBox);
this.matchPanel.Location = new System.Drawing.Point(15, 59);
this.matchPanel.Name = "matchPanel";
this.matchPanel.Size = new System.Drawing.Size(132, 36);
this.matchPanel.TabIndex = 17;
// 
// matchCaseCheckBox
// 
this.matchCaseCheckBox.AutoSize = true;
this.matchCaseCheckBox.Location = new System.Drawing.Point(0, 18);
this.matchCaseCheckBox.Name = "matchCaseCheckBox";
this.matchCaseCheckBox.Size = new System.Drawing.Size(82, 17);
this.matchCaseCheckBox.TabIndex = 7;
this.matchCaseCheckBox.Text = "Match ca&se";
this.matchCaseCheckBox.UseVisualStyleBackColor = true;
// 
// matchWordCheckBox
// 
this.matchWordCheckBox.AutoSize = true;
this.matchWordCheckBox.Location = new System.Drawing.Point(0, 0);
this.matchWordCheckBox.Name = "matchWordCheckBox";
this.matchWordCheckBox.Size = new System.Drawing.Size(135, 17);
this.matchWordCheckBox.TabIndex = 6;
this.matchWordCheckBox.Text = "Match w&hole word only";
this.matchWordCheckBox.UseVisualStyleBackColor = true;
// 
// FindAndReplace
// 
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(422, 155);
this.Controls.Add(this.matchPanel);
this.Controls.Add(this.directionGroupBox);
this.Controls.Add(this.replaceComboBox);
this.Controls.Add(this.replaceWithLabel);
this.Controls.Add(this.rangeGroupBox);
this.Controls.Add(this.closeButton);
this.Controls.Add(this.replaceAllButton);
this.Controls.Add(this.replaceButton);
this.Controls.Add(this.findButton);
this.Controls.Add(this.findComboBox);
this.Controls.Add(this.findWhatLabel);
this.MaximizeBox = false;
this.Name = "FindAndReplace";
this.Text = "Replace";
this.Load += new System.EventHandler(this.ReplaceForm_Load);
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.ReplaceForm_FormClosing);
this.rangeGroupBox.ResumeLayout(false);
this.rangeGroupBox.PerformLayout();
this.directionGroupBox.ResumeLayout(false);
this.directionGroupBox.PerformLayout();
this.matchPanel.ResumeLayout(false);
this.matchPanel.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label findWhatLabel;
        private System.Windows.Forms.ComboBox findComboBox;
        private System.Windows.Forms.Button findButton;
        private System.Windows.Forms.Button replaceButton;
        private System.Windows.Forms.Button replaceAllButton;
        private System.Windows.Forms.Button closeButton;
        private System.Windows.Forms.GroupBox rangeGroupBox;
        private System.Windows.Forms.RadioButton rangeAllFiles;
        private System.Windows.Forms.RadioButton rangeWholeFile;
        private System.Windows.Forms.RadioButton rangeSelection;
        private System.Windows.Forms.ComboBox replaceComboBox;
        private System.Windows.Forms.Label replaceWithLabel;
        private System.Windows.Forms.GroupBox directionGroupBox;
        private System.Windows.Forms.RadioButton directionDown;
        private System.Windows.Forms.RadioButton directionUp;

        private bool foundOnce = false;
        private Panel matchPanel;
        private CheckBox matchCaseCheckBox;
        private CheckBox matchWordCheckBox;
        private MainForm frm;

        public FindAndReplace(FindType findType)
        {
            InitializeComponent();
            if (findType == FindType.FindOnly)
            {
                replaceWithLabel.Hide();
                replaceComboBox.Hide();
                replaceButton.Hide();
                replaceAllButton.Hide();
                rangeGroupBox.Hide();
                matchPanel.Location = replaceWithLabel.Location;
                closeButton.Location = replaceButton.Location;
                directionGroupBox.Location = new Point(rangeGroupBox.Location.X, matchPanel.Location.Y);
                ClientSize = new Size(ClientSize.Width, ClientSize.Height - directionGroupBox.Height - replaceWithLabel.Height);
                Text = "Find";
            }
        }

        private void ReplaceForm_Load(object sender, EventArgs e)
        {
            frm = (MainForm)Application.OpenForms["MainForm"];
        }

        private void ReplaceForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            frm.TextToFind = findComboBox.Text;
            frm.TextToReplace = replaceComboBox.Text;
        }

        private void findComboBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                findButton_Click(new object(), new EventArgs());
        }

        private void findComboBox_TextChanged(object sender, EventArgs e)
        {
            foundOnce = false;
        }

        private void findButton_Click(object sender, EventArgs e)
        {
            Document doc = frm.ActiveDocument();
            if (doc != null)
            {
                int iFound = doc.Find(findComboBox.Text,
                    directionDown.Checked,
                    matchWordCheckBox.Checked,
                    matchCaseCheckBox.Checked);

                // Found
                if (iFound >= 0)
                {
                    foundOnce = true;
                    frm.Select();
                    doc.Activate();
                    findButton.Text = "&Find Next";
                }

                // Not found
                if (iFound < 0)
                {
                    if (!foundOnce)
                    {
                        MBox.Info("Searching string not found", Text);
                    }
                    else
                    {
                        if (MBox.Confirm("Search string not found, restart from the beginning?", Text))
                        {
                            doc.GoTop();
                            findButton_Click(new object(), new EventArgs());
                        }
                        else
                        {
                            foundOnce = false;
                        }
                    }
                }
            }
        }

        private void replaceButton_Click(object sender, EventArgs e)
        {
            if (!foundOnce)
                findButton_Click(sender, e);

            Document doc = frm.ActiveDocument();
            if (doc != null)
            {
                if (doc.SelectedText.Length > 0)
                {
                    doc.ReplaceSelectedText(replaceComboBox.Text);
                    findButton_Click(sender, e);
                }
            }
        }

        private void replaceAllButton_Click(object sender, EventArgs e)
        {
            if (rangeSelection.Checked)
            {
                replaceButton_Click(sender, e);
                return;
            }

            int itemsCount = 0, tempCount = 0, filesCount = 0;
            if (rangeAllFiles.Checked)
            {
                foreach (TabPage tp in frm.TabPages)
                {
                    if (tp is Document)
                    {
                        Document doc = (Document)tp;
                        tempCount = ReplaceAll(doc);
                        if (tempCount > 0)
                        {
                            itemsCount += tempCount;
                            filesCount++;
                        }
                    }
                }
            }
            else //if (rangeWholeFile.Checked)
            {
                itemsCount = ReplaceAll(frm.ActiveDocument());
            }

            if (itemsCount > 0)
            {
                if (filesCount > 0)
                    MBox.Info(itemsCount.ToString() + " items in " + filesCount.ToString() + " files has been replaced.", Text);
                else
                    MBox.Info(itemsCount.ToString() + " items has been replaced.", Text);
            }
        }

        private int ReplaceAll(Document doc)
        {
            if (doc != null)
            {
                int i = 0;
                doc.GoTo(1, 0);
                while (doc.FindAndReplace(findComboBox.Text,
                    replaceComboBox.Text,
                    directionDown.Checked,
                    matchWordCheckBox.Checked,
                    matchCaseCheckBox.Checked) >= 0)
                {
                    i++;
                }
                return i;
            }
            return 0;
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            Close();
        }

    }
}
